package com.example.l15_yanweicheng;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;

import androidx.core.app.NotificationCompat;

public class MusicService extends Service {
    MediaPlayer mediaPlayer = new MediaPlayer();
    LocalBinder binder = new LocalBinder();
    NotificationCompat.Builder builder;
    String CHANNEL_ID = "com.example.l15_yanweicheng.music_channel";

    public class LocalBinder extends Binder {
        public void startMediaPlayer(){
            mediaPlayer.start();
        }
        public void pauseMediaPlayer(){
            mediaPlayer.pause();
        }
        public void forwardMediaPlayer(){
            mediaPlayer.seekTo(mediaPlayer.getCurrentPosition()+30000);
        }
        public void stopMediaPlayer(){
            mediaPlayer.stop();
            try {
                mediaPlayer.prepare();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        LocalBinder binder = new LocalBinder();
        return binder;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mediaPlayer.stop();
        mediaPlayer.reset();
        mediaPlayer.release();
        mediaPlayer=null;
    }

    private void createNormalNotificationChannel(){
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            CharSequence name = "Music Channel";
            String description = "This is a music notification channel!";
            int important = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID,name,important);
            channel.setDescription(description);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        createNormalNotificationChannel();

        Intent startMusic = new Intent();
        startMusic.setAction("com.example.l15_yanweicheng.control_music");
        startMusic.putExtra("control_music","start_music");
        PendingIntent startMusicPendingIntent = PendingIntent.getBroadcast(MusicService.this,1,startMusic,0);

        Intent pauseMusic = new Intent();
        pauseMusic.setAction("com.example.l15_yanweicheng.control_music");
        pauseMusic.putExtra("control_music","pause_music");
        PendingIntent pauseMusicPendingIntent = PendingIntent.getBroadcast(MusicService.this,2,pauseMusic,0);

        Intent forwardMusic = new Intent();
        forwardMusic.setAction("com.example.l15_yanweicheng.control_music");
        forwardMusic.putExtra("control_music","forward_music");
        PendingIntent forwardMusicPendingIntent = PendingIntent.getBroadcast(MusicService.this,3,forwardMusic,0);

        Intent stopMusic = new Intent();
        stopMusic.setAction("com.example.l15_yanweicheng.control_music");
        stopMusic.putExtra("control_music","stop_music");
        PendingIntent stopMusicPendingIntent = PendingIntent.getBroadcast(MusicService.this,4,stopMusic,0);

        Intent closeMusic = new Intent();
        closeMusic.setAction("com.example.l15_yanweicheng.control_music");
        closeMusic.putExtra("control_music","close_music");
        PendingIntent closeMusicPendingIntent = PendingIntent.getBroadcast(MusicService.this,5,closeMusic,0);

        builder = new NotificationCompat.Builder(this,CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_baseline_music_note_24)
                .addAction(R.drawable.ic_baseline_play_arrow_24,"Play",startMusicPendingIntent)
                .addAction(R.drawable.ic_baseline_pause_24,"Pause",pauseMusicPendingIntent)
                .addAction(R.drawable.ic_baseline_forward_30_24,"Forward 10s",forwardMusicPendingIntent)
                .addAction(R.drawable.ic_baseline_stop_24,"Stop",stopMusicPendingIntent)
                .addAction(R.drawable.ic_baseline_close_24,"Close",closeMusicPendingIntent)
                .setContentTitle("The Burning Legion")
                .setContentText("The Burning Legion————Russell Brower")
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.music_icon))
                .setStyle(new androidx.media.app.NotificationCompat.MediaStyle());
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mediaPlayer = MediaPlayer.create(this,R.raw.music);
        startForeground(1,builder.build());
        return super.onStartCommand(intent, flags, startId);
    }
}